# coding=utf-8

from MQ import Consumer,Producer
from Download import Browser,Request

IP="127.0.0.1"

if __name__ == '__main__':

    p = Producer(IP)
    p.declare_queue("content")

    #download = Browser()
    download = Request()

    def callback(channel, method, properties, msg):  # 四个参数为标准格式
        #print(channel, method, properties)  # 打印看一下是什么
        # 管道内存对象  内容相关信息  后面讲
        print(" [x] Received %r" % msg[:40])

        url = str(msg, encoding = "utf-8")
        #if url.startswith("http://") == False:
        #    url = "http://"+url
        #time.sleep(15)
        html = download.get(url)
        if html is None:
            print("Error url %r" % url)
        else:
            p.send("content",url + "\n" + html)

        channel.basic_ack(delivery_tag=method.delivery_tag)  # 告诉生成者，消息处理完成

    c = Consumer(IP)
    c.run("url",callback=callback)